Skip to content

fix(calibration): validate fitting inputs and save maps atomically - #846

Merged
NandhaKishorM merged 2 commits into
NandhaKishorM:mainfrom
antonio-mello-ai:codex/atomic-calibration
Oct 2, 2026
Merged

NandhaKishorM merged 2 commits into
NandhaKishorM:mainfrom
antonio-mello-ai:codex/atomic-calibration

Conversation

@antonio-mello-ai

Copy link
Copy Markdown
Contributor

What

save_calibration now writes to a temporary file in the destination directory, flushes and syncs it, then replaces the destination. A failed save keeps the previous file intact. Existing permissions and symlink targets are preserved, and temporary files are cleaned up on failure.

The calibration fitter now rejects malformed records before optimization: fractional or boolean qtype/k, invalid widths, matrices, non-finite values, mismatched vector lengths, and targets that are negative or do not sum to one. Soft probability targets, NumPy integer indices, and explicitly padded vectors remain supported. fit_one_temperature uses the same checks, including below the sample floor.

Why

The previous save opened the destination with "w" before serialization finished. A failed write could erase a working calibration file, and two concurrent writers could leave invalid JSON. The fitter also silently truncated mismatched vectors and converted fractional indices with int().

This completes the validation and persistence follow-up discussed in #19. #764 already handles payload structure on load; its clamp-and-warn behavior is unchanged. This PR does not include the gradient change in #832.

How it was verified

On macOS with Python 3.12:

export USE_TF=0 USE_TORCH=1 OMP_NUM_THREADS=1
python tests/test_calibrate.py                 # 176 passed
python tests/test_calibration_persistence.py   # 6 tests, OK
python tests/test_temperature_loading.py       # 7 tests, OK
python tests/test_load_errors.py               # 23 passed
python tests/test_packaging.py                 # 126 passed
python tests/test_router.py                    # 665 passed
python tests/test_criteria.py                  # 190 passed
python tests/test_hooks.py                     # 231 passed
python tests/test_hooks_api.py                 # 380 passed
ruff check laya/ --select=E9,F63,F7,F82,F401,F811 --line-length=120
python -m compileall -q laya/ tests/
git diff --check

Failure tests cover partial serialization, fsync, replacement, and a failed first save. They check the previous bytes and temporary-file cleanup. A barrier coordinates two writers before replacement; the resulting JSON must equal one complete payload. The concurrent-writer test produced invalid JSON on the unpatched code.

A comparison with main produced identical fitted maps for valid synthetic records with soft targets. The public signatures, payload format, and inference calculations are unchanged. No checkpoints were downloaded.

Checklist

  • Focused on calibration validation and persistence
  • Rebased on the latest main
  • Tests pass locally
  • Public API signatures unchanged

@Bruce-Yii Bruce-Yii left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The data-loss fix and the validation are both sound. I verified them on d853cf5 against current main (4aa6761), and #832 merges into this cleanly.

With only your new tests on main, a save that dies part-way through serialization leaves the old file as {"temperature": [ — the working map is gone. On your head a failure at serialization, fsync or replace propagates, leaves the previous bytes intact and leaves no temp file behind. On main all sixteen malformed shapes in your new table are silently accepted — unequal vectors truncated, 1.9 coerced to 1, (True, ...) read as qtype 1, NaN and unnormalised targets passing through; on your head each raises. For input that should not change, I fitted from targets that are the model's own softmax at a known temperature: 0.9 → 0.9009, 1.7 → 1.6974, 2.6 → 2.5947, identical to main, with soft targets, np.int64 indices and padded vectors still working.

The blocker is the test rather than the change. The concurrent-writer check aborts tests/test_calibrate.py on Windows — 18 of 20 runs here end in PermissionError: [WinError 5] from os.replace. Two simultaneous os.replace calls against the same target is an NTFS limitation, not something this rewrite introduced: it reproduces from a bare two-thread os.replace with no Laya code involved. ci.yml's tests (windows) job runs test_calibrate, so merging as-is turns that job red.

Asserting the invariant rather than the absence of an error keeps the check meaningful on both platforms: after two barriered writers the destination must parse as one complete payload, and a writer that lost the race is allowed to raise. The atomicity itself holds on Windows either way — I confirmed the destination stays a complete payload even when one replace loses.

Signed-off-by: Antonio <ajgcvm@gmail.com>
@NandhaKishorM
NandhaKishorM merged commit 093c0a6 into NandhaKishorM:main Oct 2, 2026
@NandhaKishorM

Copy link
Copy Markdown
Owner

Merged for 0.3.24.

Verified on the integrated branch rather than on the PR alone: the full shared test list plus the pytest lane green, compileall clean, both TypeScript packages green (laya-ts 597 tests, the SDK 26), and a decision diff against 0.3.23 on the real english and multilingual checkpoints (74 answers over choice, score and noul; short and long states; budgeted calls and predict_long) showing zero decision flips and a max probability delta of 0.000000.

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants